package com.lej.login.editInfo

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.map
import com.lej.core.base.BaseModel
import com.lej.core.base.BaseViewModel
import com.lej.core.common.PCacheInfo
import com.lej.core.daq.DAQEvent
import com.lej.core.router.CRouter
import com.lej.core.router.CRouter.Companion.navigation
import com.lej.core.utils.extention.formatTime
import com.lej.core.utils.extention.toast
import com.lej.dataservice.club.bean.login.PushInfoReq
import com.lej.dataservice.club.http.base.toBody
import com.lej.dataservice.club.http.checkData
import com.lej.dataservice.club.http.handleMsg
import com.lej.login.http.loginApiService

class EditInfoViewModel : BaseViewModel<BaseModel>() {
    var phone = ""
    var token = ""
    val nick = MutableLiveData("")
    val gender = MutableLiveData(-1)
    val headPhotoKey = MutableLiveData<String>()

    val birthdayStamp = MutableLiveData(0L)

    val birthday = birthdayStamp.map {
        if (it == 0L) "" else it.formatTime("yy-MM-dd")
    }


    suspend fun pushInfo() {
        val stamp = birthdayStamp.value
        if (stamp == null || stamp == 0L) {
            "请选择生日".toast()
            return
        }
        val gender = gender.value
        if (gender == null || gender == -1) {
            "请先选择性别".toast()
            return
        }
        val nick = nick.value
        if (nick.isNullOrEmpty()) {
            "请先输入昵称".toast()
            return
        }
        val headPhotoKey = headPhotoKey.value
        if (headPhotoKey.isNullOrEmpty()) {
            "请先上传头像".toast()
            return
        }
        showDialog("loading...")
        val body = PushInfoReq(
            avatar = headPhotoKey,
            birthday = stamp,
            code = "",
            gender = gender.toString(),
            nickname = nick,
            phone = phone,
            token = token
        ).toBody()
        kotlin.runCatching {
            val res = loginApiService.pushInfo(body).checkData()
            PCacheInfo.token = res.jsonWebToken ?: ""
            DAQEvent.login_commit_success.mobEvent("start")
            CRouter.newMain.navigation()
            ///云信登录
//            val login = NIM.login(res.accid ?: "", res.yxToken ?: "")
//            when (login.first) {
//                0 -> {
//                    PCacheInfo.token = res.jsonWebToken?:""
//                    DAQEvent.login_commit_success.mobEvent("start")
//                    (lifecycleProvider as? AppCompatActivity)?.router(CRouter.main)
//                }
//                302, 404 -> {
//                    "帐号或密码错误".toast()
//                    DAQEvent.login_commit_failure.mobEvent("account or password error")
//                }
//                else -> {
//                    "登录失败: ${login.second}".toast()
//                    DAQEvent.login_commit_failure.mobEvent("${login.first}:${login.second}")
//                }
//            }
        }.onFailure {
            it.handleMsg.toast()
        }
        dismissDialog()
    }

}